package com.growthsee.rider.component;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.growthsee.rider.domain.RiderCourse;
import com.growthsee.rider.domain.RiderCourseExtend;
import com.growthsee.rider.enumeration.CourseTypeEnum;
import com.growthsee.rider.enumeration.YesNoEnum;
import com.growthsee.rider.service.RiderCourseExtendService;
import com.growthsee.rider.service.RiderCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class InitProject implements ApplicationRunner {

    @Autowired
    private RiderCourseService riderCourseService;
    @Autowired
    private RiderCourseExtendService riderCourseExtendService;
    @Override
    public void run(ApplicationArguments args) throws Exception {
        addSystemCourse();
    }
    //初始化系统课程
    private void addSystemCourse(){
        System.out.println("项目启动了");
        int count = riderCourseService.count(Wrappers.<RiderCourse>lambdaQuery()
                .eq(RiderCourse::getType, CourseTypeEnum.PAPER.getValue())
                .eq(RiderCourse::getDeleted, YesNoEnum.NO.getValue())
        );
        if(count==0){
            RiderCourse riderCourse = new RiderCourse();
            riderCourse.setCreateBy("admin");
            riderCourse.setCreateTime(new Date());
            riderCourse.setType(CourseTypeEnum.PAPER.getValue());
            riderCourse.setTypeCode("");
            riderCourse.setName("入职考卷");
            riderCourse.setDeleted(YesNoEnum.NO.getValue());
            riderCourseService.save(riderCourse);
            RiderCourseExtend extend = new RiderCourseExtend();
            extend.setId(riderCourse.getId());
            extend.setContent("入职开始卷");
            riderCourseExtendService.save(extend);
        }
    }

}
